package com.entmanage.base.collegeco.service.impl;

import java.util.List;

import com.entmanage.base.collegeco.domain.CollegecoInternEvalScore;
import com.entmanage.base.collegeco.mapper.CollegecoEvalTemplateMapper;
import com.entmanage.base.collegeco.mapper.CollegecoInternEvaluationMapper;
import com.entmanage.base.collegeco.service.ICollegecoInternEvaluationService;
import com.entmanage.base.collegeco.domain.CollegecoEvalTemplate;
import com.entmanage.common.utils.DateUtils;
import com.entmanage.common.utils.SecurityUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import com.entmanage.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.entmanage.base.collegeco.domain.CollegecoInternEvaluation;

/**
 * 实习生成绩鉴定主Service业务层处理
 *
 * @author lsq
 * @date 2025-08-01
 */
@Service
public class CollegecoInternEvaluationServiceImpl implements ICollegecoInternEvaluationService
{
    @Autowired
    private CollegecoInternEvaluationMapper hrmInternEvaluationMapper;

    /**
     * 查询实习生成绩鉴定主
     *
     * @param evaluationId 实习生成绩鉴定主主键
     * @return 实习生成绩鉴定主
     */
    @Override
    public CollegecoInternEvaluation selectHrmInternEvaluationByEvaluationId(Long evaluationId)
    {
        return hrmInternEvaluationMapper.selectHrmInternEvaluationByEvaluationId(evaluationId);
    }

    /**
     * 查询实习生成绩鉴定主列表
     *
     * @param hrmInternEvaluation 实习生成绩鉴定主
     * @return 实习生成绩鉴定主
     */
    @Override
    public List<CollegecoInternEvaluation> selectHrmInternEvaluationList(CollegecoInternEvaluation hrmInternEvaluation)
    {
        return hrmInternEvaluationMapper.selectHrmInternEvaluationList(hrmInternEvaluation);
    }

    /**
     * 新增实习生成绩鉴定主
     *
     * @param hrmInternEvaluation 实习生成绩鉴定主
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHrmInternEvaluation(CollegecoInternEvaluation hrmInternEvaluation)
    {
        // 保存用户和部门id
        hrmInternEvaluation.setUserId(SecurityUtils.getUserId());
        hrmInternEvaluation.setDeptId(SecurityUtils.getDeptId());
        hrmInternEvaluation.setCreateTime(DateUtils.getNowDate());
        int rows = hrmInternEvaluationMapper.insertHrmInternEvaluation(hrmInternEvaluation);
        insertHrmInternEvalScore(hrmInternEvaluation);
//        insertHrmEvalTemplate(hrmInternEvaluation);
        return rows;
    }

    /**
     * 修改实习生成绩鉴定主
     *
     * @param hrmInternEvaluation 实习生成绩鉴定主
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHrmInternEvaluation(CollegecoInternEvaluation hrmInternEvaluation)
    {
        hrmInternEvaluation.setUpdateTime(DateUtils.getNowDate());
        hrmInternEvaluationMapper.deleteHrmInternEvalScoreByEvaluationId(hrmInternEvaluation.getEvaluationId());
        insertHrmInternEvalScore(hrmInternEvaluation);
        hrmInternEvaluationMapper.deleteHrmEvalTemplateByTemplateId(hrmInternEvaluation.getEvaluationId());
//        insertHrmEvalTemplate(hrmInternEvaluation);
        return hrmInternEvaluationMapper.updateHrmInternEvaluation(hrmInternEvaluation);
    }

    /**
     * 批量删除实习生成绩鉴定主
     *
     * @param evaluationIds 需要删除的实习生成绩鉴定主主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHrmInternEvaluationByEvaluationIds(Long[] evaluationIds)
    {
        hrmInternEvaluationMapper.deleteHrmInternEvalScoreByEvaluationIds(evaluationIds);
        hrmInternEvaluationMapper.deleteHrmEvalTemplateByTemplateIds(evaluationIds);
        return hrmInternEvaluationMapper.deleteHrmInternEvaluationByEvaluationIds(evaluationIds);
    }

    /**
     * 删除实习生成绩鉴定主信息
     *
     * @param evaluationId 实习生成绩鉴定主主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHrmInternEvaluationByEvaluationId(Long evaluationId)
    {
        hrmInternEvaluationMapper.deleteHrmInternEvalScoreByEvaluationId(evaluationId);
        hrmInternEvaluationMapper.deleteHrmEvalTemplateByTemplateId(evaluationId);
        return hrmInternEvaluationMapper.deleteHrmInternEvaluationByEvaluationId(evaluationId);
    }

    /**
     * 新增实习生成绩打分明细信息
     *
     * @param hrmInternEvaluation 实习生成绩鉴定主对象
     */
    public void insertHrmInternEvalScore(CollegecoInternEvaluation hrmInternEvaluation)
    {
        List<CollegecoInternEvalScore> hrmInternEvalScoreList = hrmInternEvaluation.getHrmInternEvalScoreList();
        List<CollegecoInternEvalScore> hrmInternEvalScoreTeacherList = hrmInternEvaluation.getHrmInternEvalScoreTeacherList();
        Long evaluationId = hrmInternEvaluation.getEvaluationId();

        // 批量插入集合
        List<CollegecoInternEvalScore> allScores = new ArrayList<>();

        /**
         * 新增实习生成绩打分明细信息(企业)
         */
        if (StringUtils.isNotNull(hrmInternEvalScoreList))
        {
            for (CollegecoInternEvalScore score : hrmInternEvalScoreList) {
                score.setEvaluationId(evaluationId);
                score.setType("company");
                allScores.add(score);
            }
        }
        /**
         * 新增实习生成绩打分明细信息(教师)
         */
        if (StringUtils.isNotNull(hrmInternEvalScoreTeacherList))
        {
            for (CollegecoInternEvalScore score : hrmInternEvalScoreTeacherList) {
                score.setEvaluationId(evaluationId);
                score.setType("teacher");
                allScores.add(score);
            }
        }

        // ✅ 统一插入（推荐）
        if (!allScores.isEmpty()) {
            hrmInternEvaluationMapper.batchHrmInternEvalScore(allScores);
        }
    }

//    /**
//     * 新增实习评分标准模板库（主）信息
//     *
//     * @param hrmInternEvaluation 实习生成绩鉴定主对象
//     */
//    public void insertHrmEvalTemplate(CollegecoInternEvaluation hrmInternEvaluation)
//    {
//        List<CollegecoEvalTemplate> hrmEvalTemplateList = hrmInternEvaluation.getHrmEvalTemplateList();
//        Long evaluationId = hrmInternEvaluation.getEvaluationId();
//        if (StringUtils.isNotNull(hrmEvalTemplateList))
//        {
//            List<CollegecoEvalTemplate> list = new ArrayList<CollegecoEvalTemplate>();
//            for (CollegecoEvalTemplate hrmEvalTemplate : hrmEvalTemplateList)
//            {
//                hrmEvalTemplate.setTemplateId(evaluationId);
//                list.add(hrmEvalTemplate);
//            }
//            if (list.size() > 0)
//            {
//                hrmInternEvaluationMapper.batchHrmEvalTemplate(list);
//            }
//        }
//    }
}

